/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spud.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author bsmith
 */
public class SymbolExchangeUtils {
    // allow for passing in file name aswell (cba, cba.ax, cba.ax.csv cba.csv)

    private static Pattern fileNamePattern1 = Pattern.compile("(\\w+)\\.(\\w+)\\.(csv)");
    private static Pattern fileNamePattern2 = Pattern.compile("(\\w+)\\.(csv)");
    private static Pattern fileNamePattern3 = Pattern.compile("(\\w+)\\.(\\w+)");
    private static Pattern fileNamePattern4 = Pattern.compile("(\\w+)");

    public class SymbolExchange {

        String symbol;
        String exchange;
        String fileNameExtension;

        public SymbolExchange() {
            symbol = "";
            exchange = "";
            fileNameExtension = "";
        }

        public SymbolExchange(String symbol, String exchange, String fileNameExtension) {
            this.symbol = symbol;
            this.exchange = exchange;
            this.fileNameExtension = fileNameExtension;
        }

        public String getExchange() {
            return exchange;
        }

        public void setExchange(String exchange) {
            this.exchange = exchange;
        }

        public String getSymbol() {
            return symbol;
        }

        public void setSymbol(String symbol) {
            this.symbol = symbol;
        }

        public String getFileNameExtension() {
            return fileNameExtension;
        }

        public void setFileNameExtension(String fileNameExtension) {
            this.fileNameExtension = fileNameExtension;
        }

        @Override
        public boolean equals(Object a) {
            if (a instanceof SymbolExchange) {
                SymbolExchange b = (SymbolExchange) a;
                if (b.getSymbol().equals(this.getSymbol()) && b.getExchange().equals(this.getExchange()) && b.getFileNameExtension().equals(this.getFileNameExtension())) {
                    return true;
                }
            }

            return false;
        }

        @Override
        public int hashCode() {
            return symbol.hashCode() + (10 * exchange.hashCode()) + (100 * fileNameExtension.hashCode());
        }

        @Override
        public String toString() {
            String a = this.getSymbol() + ", exchange: " + this.getExchange() + " (file name extension: " + this.getFileNameExtension() + ")";
            return a;
        }
    }

    /**
     *
     * @param combined - symbol[.exchange][.csv]
     * @return the parts in a SymbolExchange object, nor null if can't match
     * private static Pattern fileNamePattern =
     *
     */
    public SymbolExchange getSymbolExchange(String combined) {
        Matcher m = fileNamePattern1.matcher(combined);
        SymbolExchange symbolExchange = null;
        if ((symbolExchange = getSymbolExchangeTry1(combined)) != null) {
            return symbolExchange;
        }
        if ((symbolExchange = getSymbolExchangeTry2(combined)) != null) {
            return symbolExchange;
        }
        if ((symbolExchange = getSymbolExchangeTry3(combined)) != null) {
            return symbolExchange;
        }
        if ((symbolExchange = getSymbolExchangeTry4(combined)) != null) {
            return symbolExchange;
        }

        return null;
    }

    // Pattern fileNamePattern1 = Pattern.compile("(\\w+)\\.(\\w+)\\.(csv)");
    private SymbolExchange getSymbolExchangeTry1(String combined) {
        Matcher m = fileNamePattern1.matcher(combined);

        if (m.matches()) {
            SymbolExchange symbolExchange = this.new SymbolExchange();

            symbolExchange.setSymbol(m.group(1));
            symbolExchange.setExchange(m.group(2));
            symbolExchange.setFileNameExtension(m.group(3));
            return symbolExchange;
        } else {
            return null;
        }
    }

    // Pattern fileNamePattern2 = Pattern.compile("(\\w+)\\.(csv)");
    private SymbolExchange getSymbolExchangeTry2(String combined) {
        Matcher m = fileNamePattern2.matcher(combined);

        if (m.matches()) {
            SymbolExchange symbolExchange = this.new SymbolExchange();

            symbolExchange.setSymbol(m.group(1));
            symbolExchange.setExchange(getDefaultExchange());
            symbolExchange.setFileNameExtension(m.group(2));
            return symbolExchange;
        } else {
            return null;
        }
    }

    // Pattern fileNamePattern3 = Pattern.compile("(\\w+)\\.(\\w+)");
    private SymbolExchange getSymbolExchangeTry3(String combined) {
        Matcher m = fileNamePattern3.matcher(combined);

        if (m.matches()) {
            SymbolExchange symbolExchange = this.new SymbolExchange();

            symbolExchange.setSymbol(m.group(1));
            symbolExchange.setExchange(m.group(2));
            symbolExchange.setFileNameExtension("");
            return symbolExchange;
        } else {
            return null;
        }
    }

    // Pattern fileNamePattern4 = Pattern.compile("(\\w+)");
    private SymbolExchange getSymbolExchangeTry4(String combined) {
        Matcher m = fileNamePattern4.matcher(combined);

        if (m.matches()) {
            SymbolExchange symbolExchange = this.new SymbolExchange();

            symbolExchange.setSymbol(m.group(1));
            symbolExchange.setExchange(getDefaultExchange());
            symbolExchange.setFileNameExtension("");
            return symbolExchange;
        } else {
            return null;
        }
    }

    public static String getDefaultExchange() {
        // todo - make it locale aware
        return "ax";
    }
}
